package com.infore.siteManagement.OutSide.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.OutSide.Service.ISparePartService;
import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.externalInterface.IStationManagement;
import com.infore.siteManagement.repository.IStationRepository;
import com.infore.siteManagement.repository.ISysUserStationRepository;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.ObjectMapperUtil;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class SparePartServiceImpl implements ISparePartService {

	@Autowired
	private ISysUserStationRepository iSysUserStationRepository;
	@Autowired
	private IStationManagement iStationManagement;
	@Autowired
	private ObjectMapperUtil objectMapperUtil;
	@Autowired
	private IStationRepository iStationRepository;

	public ResultEntity queryContStatByUser(String userIds) {
		// 查询用户下站点
		List<String> userIdList = ArrayUtils.stringToList(userIds);
		List<Map<String, Object>> stationInfos = iSysUserStationRepository.querystationForSpareByUserId(userIdList);
		List<String> mns = new ArrayList<String>();
		mns = stationInfos.stream().filter(f -> f.get("STATION_MN") != null).map(m -> m.get("STATION_MN").toString())
				.collect(Collectors.toList());
		// 查询所有站点关联的合同
		ResultEntity resultEntity = iStationManagement.queryContStat(mns);
		List<Map<String, Object>> contracts = objectMapperUtil.getMapList(resultEntity);
		Map<String, Set<String>> contrAndMn = new HashMap<String, Set<String>>();
		for (Map<String, Object> map : contracts) {
			String mn = map.get("STATION_MN").toString();
			String itemName = map.get("ITEM_NAME").toString();
			Set<String> set = new HashSet<String>();
			if (!contrAndMn.containsKey(mn)) {
				set.add(itemName);
				contrAndMn.put(mn, set);
			} else {
				set = contrAndMn.get(mn);
				set.add(itemName);
			}
		}
		// 根据站点负责人分组
		Map<String, List<Map<String, Object>>> stationInfosG = stationInfos.stream()
				.collect(Collectors.groupingBy(g -> g.get("USER_ID").toString()));
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		stationInfosG.forEach((g, glist) -> {
			Map<String, Object> map = new HashMap<String, Object>();
			Set<String> itemAndMn = new HashSet<String>();
			glist.forEach(f -> {
				Set<String> item = contrAndMn.get(f.get("STATION_MN").toString());
				if (item != null) {
					itemAndMn.addAll(item);
				}
			});
			String summary = "";
			String itemNames = "";
			if (itemAndMn != null) {
				summary = "当前共运维" + itemAndMn.size() + "个合同，" + glist.size() + "个站点";
				itemNames = StringUtils.join(itemAndMn, "、");
			}
			map.put("summary", summary);
			map.put("itemNames", itemNames);
			map.put("userId", g);
			result.add(map);
		});
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@SuppressWarnings("unchecked")
	public ResultEntity queryStationContrEquip(List<String> stationIds) {
		// 查询仪器和站点信息
		List<Map<String, Object>> equips = iStationRepository.queryBystauionId(stationIds);
		List<String> mns = new ArrayList<String>();
		mns = equips.stream().filter(f -> f.get("STATION_MN") != null).map(m -> m.get("STATION_MN").toString())
				.distinct().collect(Collectors.toList());
		// 查询所有站点关联的合同
		ResultEntity resultEntity = iStationManagement.queryContStat(mns);
		List<Map<String, Object>> contracts = objectMapperUtil.getMapList(resultEntity);
		Map<String, Object> contracAndMn = new HashMap<String, Object>();
		// 一个站点只有一个合同
		for (Map<String, Object> map : contracts) {
			String mn = map.get("STATION_MN").toString();
			contracAndMn.put(mn, map);
		}
		List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();// 返回结果集
		Map<String, Object> flag = new HashMap<String, Object>();// 判断是否已经封装过一次
		for (Map<String, Object> map : equips) {
			Map<String, Object> result = new HashMap<String, Object>();
			List<Map<String, Object>> equipment = new ArrayList<Map<String, Object>>();
			String stationName = map.get("STATION_NAME") == null ? "" : map.get("STATION_NAME").toString();// 站点名
			String mn = map.get("STATION_MN") == null ? "" : map.get("STATION_MN").toString();// mn
			String equipName = map.get("EQUIPMENT_NAME") == null ? "" : map.get("EQUIPMENT_NAME").toString();// 仪器名
			String equipModel = map.get("EQUIP_MODEL") == null ? "" : map.get("EQUIP_MODEL").toString();// 仪器模板
			String stationId = map.get("STATION_ID") == null ? "" : map.get("STATION_ID").toString();// 站点id
			// 站点下合同信息
			String itemName = "";
			String contractNum = "";
			String startTime = "";
			String endTime = "";
			String itemNum = "";
			Map<String, Object> equipSingle = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(equipModel) || StringUtils.isNotBlank(equipName)) {
				equipSingle.put("equipName", equipName);
				equipSingle.put("equipModel", equipModel);
				equipment.add(equipSingle);
			}
			Map<String, Object> contrac = (Map<String, Object>) contracAndMn.get(mn);
			if (contrac != null) {
				itemName = contrac.get("ITEM_NAME") == null ? "" : contrac.get("ITEM_NAME").toString();
				contractNum = contrac.get("CONTRACT_NUM") == null ? "" : contrac.get("CONTRACT_NUM").toString();
				endTime = contrac.get("END_TIME") == null ? "" : contrac.get("END_TIME").toString();
				startTime = contrac.get("START_TIME") == null ? "" : contrac.get("START_TIME").toString();
				itemNum = contrac.get("ITEM_NUM") == null ? "" : contrac.get("ITEM_NUM").toString();
			}
			if (!flag.containsKey(mn)) {
				result.put("mn", mn);
				result.put("itemName", itemName);
				result.put("contractNum", contractNum);
				result.put("time", startTime + "~" + endTime);
				result.put("stationName", stationName);
				result.put("itemNum", itemNum);
				result.put("stationId", stationId);
				result.put("equips", equipment);
				flag.put(mn, equipment);
				results.add(result);
			} else {
				equipment = (List<Map<String, Object>>) flag.get(mn);
				equipment.add(equipSingle);
			}
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(results);

	}

}
